﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.Utils;

namespace LinkCard.ShopCP.UploadExcel
{
    public partial class file_new : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null)
            {
                Response.Redirect("/login.aspx");
            }

            if (!Page.IsPostBack)
            {
                UploadDate.Value = DateTime.Today.ToShortDateString();
            }

        }
        

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            add();
            Response.Redirect("/UploadExcel/file_list.aspx");
        }

        protected void add()
        {
            Int64 nSupplierID = getSupplierID();
            
            string sTest ="";

            sTest = txtTest.Text;

            sTest = hash1way.SQLPatch(sTest);
            
            string sTitle = txtTitle.Value;
            
            sTitle = hash1way.SQLPatch(sTitle);
            
            string sUploadDate = ConvertUtility.ToDateTime(UploadDate.Value).ToString("yyyy/MM/dd");

            string sSQL = "Insert into FileUpload values (" + nSupplierID + ", '" + sTitle + "','" + sTest + "',0,'" + sUploadDate + "')";
            DataHelper.ExecuteNonQuery(sSQL, true);

        }

        protected Int64 getSupplierID()
        {
            try
            {

                if (Convert.ToInt32(Session["SHOPADMIN"]) == 1) // quanr ly cua hang
                {
                    return ConvertUtility.ToInt64(Session["USER"]);
                }
                else if (Convert.ToInt32(Session["SHOPADMIN"]) == 0)
                {
                    DataTable dt = DataHelper.SelectTable("SELECT SupplierID FROM [ShopStaff] WHERE Username ='" + Session["USER"].ToString() + "'", false);
                    if (dt.Rows.Count > 0)
                    {
                        return ConvertUtility.ToInt64(dt.Rows[0]["SupplierID"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}